<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/15
 * Time: 15:08
 */
namespace Admin\Controller;

use Bbs\Api\ForumAttachmentApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumPostApi;
use Think\Controller;

class BbsForumPostController extends AdminController{

    public function index(){

        $map = array(
            'status' => 1
        );
        $pid = I('get.pid',-1);
        if($pid!==-1){
            $map['pid'] = $pid;
        }
        $tid = I('get.tid',-1);
        if($tid!==-1){
            $map['tid'] = $tid;
        }

        $uid = I('uid',-1);
        if($uid>0){
            $map['author_id'] = $uid;
            $this->assign('uid',$uid);
        }

        $display = I('get.display',1);
        $this->assign('display',$display );

        $page = array('curpage' =>  I('get.p',0), 'size' => 15);
        $order = 'dateline desc';
        $params['state_invisible'] = $display ;

        $result = apiCall('Bbs/ForumPost/queryAllPost',array($map,$display,$page,$order,$params));

        if($result['status']){
            $this->assign('list',$result['info']['list']);
            $this->assign('show',$result['info']['show']);
        }else{
            $this->error($result['info']);
        }


        $this->display();

    }

    /**
     * 帖子详情
     */
    public function detail(){
        $pid = I('get.pid',-1);
        if($pid==-1){
            $this->error('参数错误');
        }
        $map = array(
            'pid' => $pid
        );
        $result = apiCall(ForumPostApi::GET_INFO,array($map));
        if($result['status']){

            $post = $result['info'];
            if(!is_null($post)){
                $post['message'] = htmlspecialchars_decode($post['message']);
            }

            //查询板块
            $result = apiCall(ForumForumApi::GET_INFO,array(array('fid'=>$post['fid'])));
            if($result['status']){
                $forum = $result['info'];
                $this->assign('forum',$forum);
            }

            //查询主题帖
            $result = apiCall(ForumPostApi::GET_INFO,array(array('tid'=>$post['tid'])));
            if($result['status']){
                $thread = $result['info'];
                $this->assign('thread',$thread);
            }

            //查询附件
            $result = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array(array('pid'=>$pid)));
            if($result['status']){
                $attachment = $result['info'];
                $this->assign('attachment',$attachment);
            }


            $this->assign('post',$post);

        }else{
            $this->error('未知错误');
        }

        $this->display();

    }

    /**
     * 通过审核
     */
    public function pass(){

       if(IS_AJAX){
           $pid = I('get.pid',-1);
           if($pid == -1){
               $this->error('参数错误');
           }

           $map = array(
               'pid' => $pid
           );
           $entity = array('invisible' => 1);
           $result = apiCall(ForumPostApi::SAVE,array($map ,$entity));
           if($result['status']){
               $this->success('操作成功!');
           }else{
               $this->error('操作失败!');
           }
       }
    }


    /**
     * 删除帖子
     */
    public function deletePost(){

        $map = array('pid' => I('pid', -1));
        $result = apiCall(ForumPostApi::DELETE,array($map));

        if($result['status']){
            $this -> success(L('RESULT_SUCCESS'));
        }else{
            LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this->error($result['info']);
        }

    }

    /**
     * 批量删除FAQ
     */
    public function bulkDeletePost(){
        $ids = I('ids', -1);
        if ($ids === -1) {
            $this -> error(L('ERR_PARAMETERS'));
        }
        $ids = implode(',', $ids);
        $map = array('pid' => array('in', $ids));

        $result = apiCall(ForumPostApi::DELETE,array($map));

        if($result['status']){
            $this -> success(L('RESULT_SUCCESS'));
        }else{
            LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this->error($result['info']);
        }

    }

}